﻿
Partial Class Content_Reportes_rep_controls_ReporteMovimientosTitulos
    Inherits System.Web.UI.UserControl
    Dim IDataBase As New Clases.DataBase
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txtFechaLiquid.Attributes.Add("onKeyPress", "return false;")
            txtFechaOp.Attributes.Add("onKeyPress", "return false;")
            txtFechaLiquid.Attributes.Add("onPaste", "return false;")
            txtFechaOp.Attributes.Add("onPaste", "return false;")
        End If
        

    End Sub


    Protected Sub btnBusqueda_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs) Handles btnBusqueda.Click
        Busqueda()
    End Sub

    Protected Sub Busqueda()
        Dim query As String = ""

        If ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
            query = "select * from vwDetalleMovT vwd, TDetalle_Movimiento_Titulos dmt, vwClientes vwc where vwD.mov_id=dmt.mov_id and dmt.cl_id=vwc.cl_id"
        Else
            query = "select * from vwDetalleMovT where 1 = 1 "

        End If


        If ddlTiposMov.SelectedValue = 1 Then
            If ddlTiposOp.SelectedValue = "Particular" Then
                query = query & " and vwD.mov_operacion = 'Compra' "
            Else
                query = query & " and mov_operacion = 'Compra' "
            End If

            If ddlTiposOp.SelectedValue = "Global" Then
                query = query & " and mov_tipo = 'Global' "
            ElseIf ddlTiposOp.SelectedValue = "Particular" Then
                query = query & " and vwD.mov_tipo = 'Particular' "
            ElseIf ddlTiposOp.SelectedValue = "Conjunto" Then
                query = query & " and mov_tipo = 'Conjunto' "
            End If

        ElseIf ddlTiposMov.SelectedValue = 2 Then
            query = query & " and mov_operacion = 'Venta' "
            If ddlTiposOp.SelectedValue = "Global" Then
                query = query & " and mov_tipo = 'Global' "
            ElseIf ddlTiposOp.SelectedValue = "Particular" Then
                query = query & " and vwD.mov_tipo 'Particular' "
            ElseIf ddlTiposOp.SelectedValue = "Conjunto" Then
                query = query & " and mov_tipo = 'Conjunto' "
            End If

        ElseIf ddlTiposMov.SelectedValue = 3 Then
            query = query & " and vwD.mov_operacion = 'Entrada Fisica' "
        ElseIf ddlTiposMov.SelectedValue = 4 Then
            query = query & " and vwD.mov_operacion = 'Salida Fisica' "
        End If

        'Se agregan los campos al filtro para la busqueda
        If txtCliente.Text <> "" And ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
            query = query & "and vwc.nombreC like '%" & txtCliente.Text & "%'"

        End If

        If txtDespacho.Text <> "" And ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
            query = query & "and vwd.Despacho like '" & txtDespacho.Text & "%'"
        ElseIf txtDespacho.Text <> "" Then
            query = query & "and Despacho like '" & txtDespacho.Text & "%'"
        End If

        If txtInstitucion.Text <> "" And ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
            query = query & "and vwd.Institución like '" & txtInstitucion.Text & "%'"
        ElseIf txtInstitucion.Text <> "" Then
            query = query & "and Institución like '" & txtInstitucion.Text & "%'"
        End If

        If txtEmisora.Text <> "" And ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
            query = query & "and vwd.Emisora like '" & txtEmisora.Text & "%'"
        ElseIf txtEmisora.Text <> "" Then
            query = query & "and Emisora like '" & txtEmisora.Text & "%'"
        End If

        'Tipo de Operacion
        'If ddlTiposOp.SelectedValue = "Particular" Or ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Then
        '    query = query & " and vwd.mov_tipo like '%%' "
        'Else
        '    query = query & " and mov_tipo = '" & ddlTiposOp.SelectedValue & "'"
        'End If

        'Fechas

        If txtFechaOp.Text IsNot "" And txtFechaLiquid.Text IsNot "" Then
            query = query & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "' AND [Fecha de Operación] <= '" & txtFechaLiquid.Text & "'"
        ElseIf txtFechaOp.Text IsNot "" Then
            query = query & " AND [Fecha de Operación] >= '" & txtFechaOp.Text & "'"

        ElseIf txtFechaLiquid.Text IsNot "" Then
            query = query & " AND [Fecha de Operación] <= '" & txtFechaLiquid.Text & "'"
        End If

        If ddlTiposMov.SelectedValue = 3 Or ddlTiposMov.SelectedValue = 4 Or ddlTiposMov.SelectedValue = 0 Or ddlTiposOp.SelectedValue = "Particular" Then

            gvConsulta.Columns(0).Visible = True
        ElseIf ddlTiposMov.SelectedValue = 1 Or ddlTiposMov.SelectedValue = 2 Then
            gvConsulta.Columns(0).Visible = False
        End If
        hf_remov.Value = query
        Session("RepMovTit") = query
        'Se llena la tabla con los resultados obtenidos
        IControles.fillGridViewPaging(gvConsulta, query)

        btn_excel.Visible = True
    End Sub

    Protected Sub ddlTiposMov_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlTiposMov.SelectedIndexChanged
        If ddlTiposMov.SelectedValue = 1 Then
            If ddlTiposOp.SelectedValue <> "Particular" Then
                txtCliente.Visible = False
            Else
                txtCliente.Visible = True
            End If
            txtFechaLiquid.Visible = True
            calFiltro2.Visible = True
            ddlTiposOp.Visible = True
        ElseIf ddlTiposMov.SelectedValue = 2 Then
            If ddlTiposOp.SelectedValue <> "Particular" Then
                txtCliente.Visible = False
            Else
                txtCliente.Visible = True
            End If
            txtFechaLiquid.Visible = True
            calFiltro2.Visible = True
            ddlTiposOp.Visible = True
        ElseIf ddlTiposMov.SelectedValue = 3 Then
            txtCliente.Visible = True
            txtFechaLiquid.Visible = False
            calFiltro2.Visible = False
            ddlTiposOp.Visible = False
        Else
            txtCliente.Visible = True
            txtFechaLiquid.Visible = True
            calFiltro2.Visible = True
            ddlTiposOp.Visible = False
        End If
    End Sub
    Protected Sub ddlTiposOp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTiposOp.SelectedIndexChanged
        If ddlTiposOp.SelectedValue <> "Particular" Then
            txtCliente.Visible = False
        Else
            txtCliente.Visible = True
        End If
    End Sub

    Protected Sub gvConsulta_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvConsulta.PageIndexChanging
        IControles.fillGridViewPaging(gvConsulta, hf_remov.Value, Convert.ToInt32(e.NewPageIndex))
    End Sub

    Protected Sub exportToExcel(ByVal query As String)
        Dim gridView_toExport As New GridView
        IControles.fillGridViewPaging(gridView_toExport, Session("RepMovTit"))

        For i As Integer = 0 To gridView_toExport.Rows.Count - 1
            Dim row As GridViewRow = gridView_toExport.Rows(i)
        Next

        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As IO.StringWriter = New IO.StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        gridView_toExport.AllowPaging = False
        form.Controls.Add(gridView_toExport)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"

        Response.AddHeader("Content-Disposition", "attachment;filename=InvDisp" & Now.ToString("_dd_MMMM_yy_HH_mm_ss") & ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    Protected Sub btn_excel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_excel.Click
        exportToExcel(Session("RepMovTit"))
    End Sub

End Class